package cn.lichenfei.jfx.music.gui;

import cn.hutool.core.lang.Console;
import cn.lichenfei.jfx.music.common.MusicPlayer;
import cn.lichenfei.jfx.music.common.utils.JFUtils;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Paint;
import org.kordamp.ikonli.antdesignicons.AntDesignIconsOutlined;
import org.kordamp.ikonli.boxicons.BoxiconsRegular;
import org.kordamp.ikonli.javafx.FontIcon;

import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author ChenFei
 * @date 2022/4/28
 */
public class MusicItem extends HBox {

    private MusicList musicList;
    private int index;
    //
    private Label iconLabel = new Label();
    private HBox nameBox = new HBox();
    private Label nameLabel = new Label();

    {
        this.getStyleClass().add("music-item");
        iconLabel.setGraphic(new FontIcon(BoxiconsRegular.MUSIC));
    }

    private ToggleButton pauseStartBut = new ToggleButton("");
    private Button deleteBut = new Button("");

    public MusicItem(String fileName, Integer index, ToggleGroup group, MusicList musicList) {
        this.musicList = musicList;
        this.index = index;
        this.nameLabel.setText(fileName);
        this.getChildren().addAll(iconLabel, nameBox, pauseStartBut, deleteBut);
        nameBox.getChildren().add(nameLabel);
        this.setHgrow(nameBox, Priority.ALWAYS);
        nameBox.setAlignment(Pos.CENTER_LEFT);
        //class
        iconLabel.getStyleClass().add("icon-label");
        pauseStartBut.getStyleClass().add("pause-start-but");
        deleteBut.getStyleClass().add("delete-but");
        //icon
        pauseStartBut.setGraphic(new FontIcon(AntDesignIconsOutlined.CARET_RIGHT));//PAUSE
        deleteBut.setGraphic(new FontIcon(AntDesignIconsOutlined.DELETE));
        //绑定组
        pauseStartBut.setToggleGroup(group);
        //监听选中事件
        pauseStartBut.selectedProperty().addListener((observableValue, aBoolean, t1) -> {
            setStyle(t1);
            MusicPane musicPane = JFUtils.getParent(musicList, MusicPane.class);
            MusicHome musicHome = musicPane.getMusicHome();
            if (t1) {
                if (index == MusicPlayer.currentIndex) {
                    musicHome.play();
                } else {
                    musicHome.playMusic(index);
                }
            } else {
                musicHome.pause();
            }
        });
    }

    /**
     * 删除事件
     *
     * @param consumer
     */
    public void deleteListener(Consumer<Integer> consumer) {
        deleteBut.setOnMouseClicked(event -> consumer.accept(index));
    }

    /**
     * 设置样式
     *
     * @param flag
     */
    private void setStyle(boolean flag) {
        FontIcon iconLabelGraphic = (FontIcon) iconLabel.getGraphic();
        if (flag) {
            pauseStartBut.setGraphic(new FontIcon(AntDesignIconsOutlined.PAUSE));
            iconLabelGraphic.setIconColor(Paint.valueOf("#fff"));
            iconLabel.setStyle("-fx-background-color:#409EFF;");
            nameLabel.setStyle("-fx-text-fill:#409EFF;");
        } else {
            pauseStartBut.setGraphic(new FontIcon(AntDesignIconsOutlined.CARET_RIGHT));
            iconLabelGraphic.setIconColor(Paint.valueOf("#303133"));
            iconLabel.setStyle("-fx-background-color: rgb(255,255,255,0.7);");
            nameLabel.setStyle("-fx-text-fill:#303133;");
        }
    }

}
